package com.insis.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
@Controller
public class CoorsController {
    @RequestMapping(value={"/save_coors"})
    @ResponseBody
    public String saveCoors(HttpServletRequest request) {
        String overlayspatharray_text = request.getParameter("overlayspathArray");
        //String counts_text = request.getParameter("counts");
        //String countsarray_text = request.getParameter("countsArray");
        //System.out.println(overlayspatharray_text);
        overlayspatharray_text = overlayspatharray_text.replace("\"", "");
        //System.out.println(overlayspatharray_text);
        File file = new File(String.valueOf(this.getClass().getResource("").getPath()) + "/result");
        System.out.println(file);
        System.out.println("以上是路径，result文件中的1为轨迹");
        if (!file.exists()) {
            file.mkdirs();
        }
        saveFile(overlayspatharray_text, 1);
        return "success";
    }

    public void saveFile(String text, int i) {
        FileOutputStream out = null;
        File outFile = new File(String.valueOf(this.getClass().getResource("").getPath()) + "/result/" + i);
        try {
            out = new FileOutputStream(outFile, false);
            out.write(text.getBytes());
            out.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @RequestMapping(value={"/save_marker_coors"})
    @ResponseBody
    public String savemarkerCoors(HttpServletRequest request) {
        String overlaysmarkerpatharray_text = request.getParameter("overlaysmarkerpathArray");
        //String counts_text = request.getParameter("counts");
        //String countsarray_text = request.getParameter("countsArray");
        //System.out.println(overlayspatharray_text);
        overlaysmarkerpatharray_text = overlaysmarkerpatharray_text.replace("\"", "");
        //System.out.println(overlayspatharray_text);
        File file = new File(String.valueOf(this.getClass().getResource("").getPath()) + "/result");
        //System.out.println(file);
        if (!file.exists()) {
            file.mkdirs();
        }
        savemarkerFile(overlaysmarkerpatharray_text, 2);
        return "success";
    }

    public void savemarkerFile(String text, int i) {
        FileOutputStream out = null;
        File outFile = new File(String.valueOf(this.getClass().getResource("").getPath()) + "/result/" + i);
        try {
            out = new FileOutputStream(outFile, false);
            out.write(text.getBytes());
            out.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }    
    
    
    
    @RequestMapping(value={"/init_coors"})
    @ResponseBody
    public List<String> initCoors(HttpServletRequest request) {
        LinkedList<String> coor_list = new LinkedList<String>();
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        File result = new File(String.valueOf(this.getClass().getResource("").getPath()) + "/result/");
        if (result.isDirectory()) {
            String[] fileNames = result.list();
            String floder_path = result.getPath();
            try {
                String fileName = fileNames[0];
                fis = new FileInputStream(new File(String.valueOf(floder_path) + "/" + "1"));
                isr = new InputStreamReader(fis, "UTF-8");
                br = new BufferedReader(isr);
                String line = "";
                while ((line = br.readLine()) != null) {
                    coor_list.add(line);
                }
            }
            catch (Exception fileName) {
                // empty catch block
            }
        }
        return coor_list;
    }
    @RequestMapping(value={"/init_marker_coors"})
    @ResponseBody
    public List<String> initmarkerCoors(HttpServletRequest request) {
        LinkedList<String> coor_list = new LinkedList<String>();
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        File result = new File(String.valueOf(this.getClass().getResource("").getPath()) + "/result/");
        if (result.isDirectory()) {
            String[] fileNames = result.list();
            String floder_path = result.getPath();
            try {
                String fileName = fileNames[0];
                fis = new FileInputStream(new File(String.valueOf(floder_path) + "/" + "2"));
                isr = new InputStreamReader(fis);
                br = new BufferedReader(isr);
                String line = "";
                while ((line = br.readLine()) != null) {
                    coor_list.add(line);
                }
            }
            catch (Exception fileName) {
                // empty catch block
            }
        }
        return coor_list;
    }
    @RequestMapping(value={"/init_marker_coors2"})
    @ResponseBody
    public List<String> initmarkerCoors2(HttpServletRequest request) {
        LinkedList<String> coor_list = new LinkedList<String>();
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        File result = new File(String.valueOf(this.getClass().getResource("").getPath()) + "/result/");
        if (result.isDirectory()) {
            String[] fileNames = result.list();
            String floder_path = result.getPath();
            try {
                String fileName = fileNames[0];
                fis = new FileInputStream(new File(String.valueOf(floder_path) + "/" + "3"));
                isr = new InputStreamReader(fis);
                br = new BufferedReader(isr);
                String line = "";
                while ((line = br.readLine()) != null) {
                    coor_list.add(line);
                }
            }
            catch (Exception fileName) {
                // empty catch block
            }
        }
        return coor_list;
    }
    
    @RequestMapping(value={"/init_poly"})
    @ResponseBody
    public List<String> initpoly(HttpServletRequest request) {
        LinkedList<String> coor_list = new LinkedList<String>();
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        File result = new File(String.valueOf(this.getClass().getResource("").getPath()) + "/result/");
        if (result.isDirectory()) {
            String[] fileNames = result.list();
            String floder_path = result.getPath();
            try {
                String fileName = fileNames[0];
                fis = new FileInputStream(new File(String.valueOf(floder_path) + "/" + "4"));
                isr = new InputStreamReader(fis, "UTF-8");
                br = new BufferedReader(isr);
                String line = "";
                while ((line = br.readLine()) != null) {
                    coor_list.add(line);
                }
            }
            catch (Exception fileName) {
                // empty catch block
            }
        }
        return coor_list;
    }
    @RequestMapping(value={"/check_coors"})
    @ResponseBody
    public List<String> checkCoors(HttpServletRequest request) {
        String date = request.getParameter("date");
        String userId = request.getParameter("userId");
        date = date.substring(1, date.length() - 1);
        System.out.println("date" + date);
        userId = userId.substring(1, userId.length() - 1);
        System.out.println(date);
        System.out.println(date.length());
        System.out.println(userId);
        LinkedList<String> coor_list = new LinkedList<String>();
        String hotDayArticle = "";
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        File result = new File(String.valueOf(this.getClass().getResource("").getPath()) + "/result/");
        if (result.isDirectory()) {
            String[] fileNames = result.list();
            String floder_path = result.getPath();
            try {
                String fileName = fileNames[0];
                fis = new FileInputStream(new File(String.valueOf(floder_path) + "/" + "每日最热文章.txt"));
                isr = new InputStreamReader(fis, "UTF-8");
                br = new BufferedReader(isr);
                String line = "";
                while ((line = br.readLine()) != null) {
                    String day = line.split(":")[0];
                    if (day.equals(date.substring(0, 10))) {
                    	hotDayArticle = line.split(":")[1];
                    	break;
                    }
                }
            }
            catch (Exception fileName) {
                // empty catch block
            }
        }


        String hotDayBlock = "";
        File result1 = new File(String.valueOf(this.getClass().getResource("").getPath()) + "/result/");
        if (result1.isDirectory()) {
            String[] fileNames = result1.list();
            String floder_path = result1.getPath();
            try {
                String fileName = fileNames[0];
                fis = new FileInputStream(new File(String.valueOf(floder_path) + "/" + "每日最热版块.txt"));
                isr = new InputStreamReader(fis, "UTF-8");
                br = new BufferedReader(isr);
                String line = "";
                while ((line = br.readLine()) != null) {
                    String day = line.split(":")[0];
                    if (day.equals(date.substring(0, 10))) {
                    	hotDayBlock = line.split(":")[1];
                    	break;
                    }
                }
            }
            catch (Exception fileName) {
                // empty catch block
            }
        }
        
        String userArticle = "";
        if (result1.isDirectory()) {
            String[] fileNames = result1.list();
            String floder_path = result1.getPath();
            try {
                String fileName = fileNames[0];
                fis = new FileInputStream(new File(String.valueOf(floder_path) + "/" + "每名用户文章推荐.txt"));
                isr = new InputStreamReader(fis, "UTF-8");
                br = new BufferedReader(isr);
                String line = "";
                while ((line = br.readLine()) != null) {
                    String day = line.split(":")[0];
                    if (day.equals(userId)) {
                    	userArticle = line.split(":")[1];
                    	break;
                    }
                }
            }
            catch (Exception fileName) {
                // empty catch block
            }
        }
        
        
        
        
        coor_list.add(hotDayArticle);
        coor_list.add(hotDayBlock);
        coor_list.add(userArticle);
    	return coor_list;
    }    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}